﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 第八周作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text += button1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.Text += button2.Text;
        }
        private void button3_Click(object sender, EventArgs e)
        {
            label1.Text += button3.Text;
        }
        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text += button4.Text;
        }
        private void button5_Click(object sender, EventArgs e)
        {
            label1.Text += button5.Text;
        }
        private void button6_Click(object sender, EventArgs e)
        {
            label1.Text += button6.Text;
        }
        private void button7_Click(object sender, EventArgs e)
        {
            label1.Text += button7.Text;
        }
        private void button8_Click(object sender, EventArgs e)
        {
            label1.Text += button8.Text;
        }
        private void button9_Click(object sender, EventArgs e)
        {
            label1.Text += button9.Text;
        }
        private void button10_Click(object sender, EventArgs e)
        {
            label1.Text += button10.Text;
        }
        private void button11_Click(object sender, EventArgs e)
        {
            //清空
            label1.Text = "";
        }
        private void button12_Click(object sender, EventArgs e)
        {
            //计算结果
            try
            {
                //计算器的单栈算法
                string s = label1.Text;
                s.Replace(" ", "");
                Stack stack = new Stack();
                double num = 0;
                char c = '+';//上一个运算符号，初始化为+
                int flag = 0;//标记变量，记录连续的运算符有多少个
                for (int i = 0; i < s.Length; i++)
                {

                    if (s[i] <= '9' && s[i] >= '0')//如果遇到数字，就加入当前到数值
                    {
                        num = num * 10 + (s[i] - '0');
                        flag = 0;
                    }
                    if (s[i] == '+' || s[i] == '-' || s[i] == '*' || s[i] == '/' || s[i] == '.' || i == s.Length - 1)
                    {//遇到加减乘除符号或到最后一个字符时，根据上一个加/减/乘/除来选择对当前数值进行何操作
                        flag++;
                        if (flag > 1) throw new myException1("表达式异常！");
                        switch (c)
                        {
                            case '+': stack.Push(num); break;
                            case '-': stack.Push(-num); break;
                            case '*': stack.Push((double)stack.Pop() * num); break;
                            case '/':
                                if (num == 0) throw new DivideByZeroException("除零异常！");
                                stack.Push((double)stack.Pop() / num); break;
                            case '.': stack.Push((double)stack.Pop() + num / Math.Pow(10, num.ToString().Length)); break;
                        }
                        num = 0;//当前数值清零
                        c = s[i];//更新上一个运算符号
                    }

                }
                double ans = 0;
                foreach (var i in stack)
                {
                    ans += (double)i;
                }
                label1.Text = ans.ToString();
            }
            catch (DivideByZeroException exception)
            {
                label1.Text = exception.Message;
            }
            catch (myException1 exception)
            {
                label1.Text = exception.Message;
            }

        }
        private void button13_Click(object sender, EventArgs e)
        {
            label1.Text += button13.Text;
        }
        private void button14_Click(object sender, EventArgs e)
        {
            label1.Text += button14.Text;
        }

        private void button15_Click(object sender, EventArgs e)
        {
            label1.Text += button15.Text;

        }
        private void button16_Click(object sender, EventArgs e)
        {
            label1.Text += button16.Text;

        }
    }

    //自定义表达式异常
    class myException1 : ApplicationException
    {
        public myException1(string message) : base(message)
        {
        }
    }

}
